﻿@using System.Data;
@using FileServer.Common;
@using UtilsBase;
@{
    ViewBag.PageTitle = "洪峰智能";

    Layout = "~/Views/Shared/PageTemplate.cshtml";
}

<script>
    $(document).ready(function () {
        
        if("@(ViewBag.order)"=="upd")
        {
            getEdit("@(ViewBag.d_guid)");
        }
        else{
            getModelList();
          //  getGroup();
            getGateWay();
        }


       
    });


    function getEdit(dguid) {
        $.post("getDevice", { "flag": Math.random(), guid: dguid }, function (result) {
            var myobj = (result.data);
            $("#d_name").val(myobj.d_name);
            $("#hid_d_model_guid").val(myobj.d_model_guid);
            $("#hid_d_gw_d_guid").val(myobj.d_gw_d_guid);
            //$("#hid_d_group_guid").val(myobj.d_group_guid);
            $("#d_remark").val(myobj.d_remark);
            $("#d_register_address").val(myobj.d_register_address);
            

            $("#d_parameter").val(myobj.d_parameter);
            $("#d_parameter_init").val(myobj.d_parameter_init);
            $("#d_channel").val(myobj.d_channel);
            
            for (var i = 0; i < document.getElementById("d_status").options.length; i++) {
                if (document.getElementById("d_status").options[i].value == myobj.d_status) {
                    document.getElementById("d_status").options[i].selected = true;
                    break;
                }
                else {
                    document.getElementById("d_status").options[i].selected = false;

                }
            }


            getModelList();
          //  getGroup();
            getGateWay();
           // checkShowGateWay(myobj.d_model_guid);
        });
    }

    function getModelList() {
        $("#d_model_guid").empty();
        $("#d_model_guid").append("<option value=''  selected='selected'>请选择</option>");
        $.post("getModelList", { "flag": Math.random() }, function (result) {
            var myobj = (result.data);
            for (var i = myobj.length - 1; i >= 0; i--) {
                $("#d_model_guid").prepend("<option value='" + myobj[i].m_guid + "'>" + myobj[i].m_code + "-" + myobj[i].m_agreementType + "-" + myobj[i].m_name + "</option>")
            }


            for (var i = 0; i < document.getElementById("d_model_guid").options.length; i++) {
                if (document.getElementById("d_model_guid").options[i].value == $("#hid_d_model_guid").val()) {
                    document.getElementById("d_model_guid").options[i].selected = true;
                    break;
                }
                else {
                    document.getElementById("d_model_guid").options[i].selected = false;

                }
            }
        });
    }

    //function getGroup() {
    //    $("#d_group_guid").empty();
    //    $("#d_group_guid").append("<option value=''  selected='selected'>请选择</option>");
    //    $.post("getGroupList", { "flag": Math.random() }, function (result) {
    //        var myobj = (result.data);
    //        for (var i = myobj.length - 1; i >= 0; i--) {
    //            $("#d_group_guid").prepend("<option value='" + myobj[i].g_guid + "'>" + myobj[i].g_name + "</option>")
    //        }

    //        for (var i = 0; i < document.getElementById("d_group_guid").options.length; i++) {
    //            if (document.getElementById("d_group_guid").options[i].value == $("#hid_d_group_guid").val()) {
    //                document.getElementById("d_group_guid").options[i].selected = true;
    //                break;
    //            }
    //            else {
    //                document.getElementById("d_group_guid").options[i].selected = false;

    //            }
    //        }

    //    });
    //}
    function getGateWay() {
        $("#d_gw_d_guid").empty();
        $("#d_gw_d_guid").append("<option value=''  selected='selected'>请选择</option>");
        $.post("getGateWayList", { "flag": Math.random(), p_guid: $("#d_p_guid").val(), gwdguid: $("#gwdguid").val() }, function (result) {
            var myobj = (result.data);
            for (var i = myobj.length - 1; i >= 0; i--) {
                $("#d_gw_d_guid").prepend("<option value='" + myobj[i].d_guid + "'>"+ myobj[i].d_code+"-" + myobj[i].d_name + "</option>")
            }

            for (var i = 0; i < document.getElementById("d_gw_d_guid").options.length; i++) {
                if (document.getElementById("d_gw_d_guid").options[i].value == $("#hid_d_gw_d_guid").val()) {
                    document.getElementById("d_gw_d_guid").options[i].selected = true;
                    break;
                }
                else {
                    document.getElementById("d_gw_d_guid").options[i].selected = false;

                }
            }


        });
    }
    function getModel(mguid) {
      
        $.post("getModel", { "flag": Math.random(), guid: mguid}, function (result) {
            var myobj = (result.data);
            if ("@(ViewBag.order)" != "upd") {
                $("#d_parameter").val(myobj.m_parameter_demo);
                $("#d_parameter_init").val(myobj.m_parameter_demo_init);
            }
        });
    }
   
    function saveAll() {
        var data = $("#addForm").serializeArray(); //自动将form表单封装成json
        $.post("saveAll", data, function (result) {
            var jsonresult = (result);
            if (jsonresult.respCode == "200") {
                window.location.href = "list?p_guid=" + $("#d_p_guid").val() + "&gwdguid=" + $("#gwdguid").val();

             // window.history.back();
            }
            else {
                alert(jsonresult.respMsg);
                return;
            }
        });

    }
</script>

<style>
    #pagelist_follow {
        width: 100%;
    }

    #pagelist_main {
        width: 100%;
    }

    .kv-item label{
        width:150px;
    }
</style>
<div id="saper-container">
    <form id="addForm" name="addForm" action="list" method="post">
        <input type="hidden" name="d_p_guid" id="d_p_guid" value="@ViewBag.d_p_guid" />
        <input type="hidden" name="gwdguid" id="gwdguid" value="@(ViewBag.gwdguid)" />

        <div id="saper-hd"></div>
        <div id="saper-bd">
            <div class="subfiled clearfix">
                <h2>设备管理</h2>
            </div>
            <div class="subfiled-content">

                <div id="" title="添加/编辑">
                    <form action="" name="addForm" id="addForm" method="post">

                        <div class="kv-item clearfix">
                            <label><span class="impInfo">*</span>设备名称：</label>
                            <div class="kv-item-content">
                                <input type="text" name="d_name" id="d_name" placeholder="设备名称" />
                                <input type="hidden" name="d_guid" id="d_guid" value="@ViewBag.d_guid" />
                            </div>
                        </div>

                        <div class="kv-item clearfix">
                            <label><span class="impInfo">*</span>状态：</label>
                            <div class="kv-item-content">
                                <select id="d_status" name="d_status" style="width:130px;">
                                    <option value="0">启用</option>
                                    <option value="1">禁用</option>
                                </select>
                                     
                            </div>
                        </div>

                        <div class="kv-item clearfix">
                            <label><span class="impInfo">*</span>型号：</label>
                            <div class="kv-item-content">
                                <select id="d_model_guid" name="d_model_guid" style="width:130px;" onchange="getModel(this.value)">
                                </select>
                                       <input type="hidden" name="hid_d_model_guid" id="hid_d_model_guid" value="" />
                            </div>
                        </div>

                        <div class="kv-item clearfix" id="divgateway">
                            <label><span class="impInfo">*</span>网关：</label>
                            <div class="kv-item-content">
                                <select id="d_gw_d_guid" name="d_gw_d_guid" style="width:130px;">
               
                                </select>
                                       <input type="hidden" name="hid_d_gw_d_guid" id="hid_d_gw_d_guid" value="" />
                            </div>
                        </div>
                        <div class="kv-item clearfix">
                            <label><span class="impInfo">*</span>通道：</label>
                            <div class="kv-item-content">
                                <input type="text" name="d_channel" id="d_channel" value="1" placeholder="第几路" />
                            </div>
                        </div>
                        <div class="kv-item clearfix">
                            <label><span class="impInfo">*</span>寄存器地址：</label>
                            <div class="kv-item-content">
                                <input type="text" name="d_register_address" id="d_register_address" value="1" placeholder="寄存器地址" />
                            </div>
                        </div>
                       @* <div class="kv-item clearfix">
                            <label><span class="impInfo"></span>设备组：</label>
                            <div class="kv-item-content">
                                <select id="d_group_guid" name="d_group_guid" style="width:130px;">
                                </select>
                                
                                <input type="hidden" name="hid_d_group_guid" id="hid_d_group_guid" value="" />
                            </div>
                        </div>
*@
                         <div class="kv-item clearfix">
                            <label><span class="impInfo"></span>备注：</label>
                            <div class="kv-item-content">
                                <input type="text" name="d_remark" id="d_remark" placeholder="备注" />
                            </div>
                        </div>

                         <div class="kv-item clearfix">
                            <label><span class="impInfo"></span>协议参数(json格式)：</label>
                            <div class="kv-item-content">
                              
                                <textarea id="d_parameter" name="d_parameter" cols="30" rows="10" style="width:320px;height:150px;"></textarea>
                                
                            </div>
                        </div>
                        <div class="kv-item clearfix">
                            <label><span class="impInfo"></span>设备初始化(json格式)：</label>
                            <div class="kv-item-content">

                                <textarea id="d_parameter_init" name="d_parameter_init" cols="30" rows="10" style="width:320px;height:150px;"></textarea>
                             <br />
                                <a href="https://www.sojson.com/" target="_blank">json在线检测工具</a>
                                &nbsp;&nbsp;
                                <a href="http://www.ip33.com/crc.html" target="_blank">modbus校验码</a>
                            </div>
                        </div>
                        <div><hr /></div>
                      
                        <div><hr /></div>
                        <div  style ="margin:0 auto;width:150px;">
                      
                            <input type="button" id="save" name="save" value="保存设备" onclick="javascript:saveAll()"  />
                            &nbsp;&nbsp; <input type="button" id="but_back1" name="but_back1" value="返回" onclick="javascript:window.history.back()" />
                        </div>
                    </form>
                </div>
            </div>
        </div>
        <div id="saper-ft"></div>
    </form>
</div>

